﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using System.Collections.ObjectModel;
using Microsoft.Phone.Shell;
using System.Windows.Media.Imaging;
using ACTT_WP7.Resources;

namespace ACTT_WP7
{
    public partial class DetailsPage : PhoneApplicationPage
    {
        // Constructor
        public DetailsPage()
        {
            InitializeComponent();
            DataContext = App.ViewModel;
            Stops_List.DataContext = App.ViewModel;
        }

        // When page is navigated to set data context to selected item in list
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string selectedIndex = "";
            if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
            {
                int index = int.Parse(selectedIndex);
                DataContext = App.ViewModel.LinesItems[index];
                Line current_line = App.ViewModel.LinesItems[index].Line;
                PivotControl.Title = current_line.Name;

                Image_a.Source = new BitmapImage(new Uri("/ACTT_WP7;component/Images/data/" + MainPage.current_city.Period+current_line.ImageUrl_A, UriKind.RelativeOrAbsolute));
                Image_b.Source = new BitmapImage(new Uri("/ACTT_WP7;component/Images/data/" + MainPage.current_city.Period + current_line.ImageUrl_B, UriKind.RelativeOrAbsolute));
                Header_a.Source = new BitmapImage(new Uri("/ACTT_WP7;component/Images/data/" + MainPage.current_city.Period + current_line.Header_A, UriKind.RelativeOrAbsolute));
                Header_b.Source = new BitmapImage(new Uri("/ACTT_WP7;component/Images/data/" + MainPage.current_city.Period + current_line.Header_B, UriKind.RelativeOrAbsolute));
                if (current_line.Stops2.Count > 0)
                {
                    App.ViewModel.StopsItems.Add(new StopsViewModel() { Name = Strings.Going });
                    App.ViewModel.StopsItems.Add(new StopsViewModel() { Name = "" });
                    for (int i = 0; i < current_line.Stops1.Count; i++)
                        App.ViewModel.StopsItems.Add(new StopsViewModel() { Name = current_line.Stops1[i] });
                    App.ViewModel.StopsItems.Add(new StopsViewModel() { Name = "" });
                    App.ViewModel.StopsItems.Add(new StopsViewModel() { Name = "" });
                    App.ViewModel.StopsItems.Add(new StopsViewModel() { Name = Strings.Return });
                    App.ViewModel.StopsItems.Add(new StopsViewModel() { Name = "" });
                    for (int i = 0; i < current_line.Stops2.Count; i++)
                        App.ViewModel.StopsItems.Add(new StopsViewModel() { Name = current_line.Stops2[i] });
                }
                else
                {
                    for (int i = 0; i < current_line.Stops1.Count; i++)
                        App.ViewModel.StopsItems.Add(new StopsViewModel() { Name = current_line.Stops1[i] });
                }

                Image_a.ImageFailed += (s, args) =>
                {
                    var s1 = MainPage.current_city.Url + current_line.ImageUrl_A;
                    Image_a.Source = new BitmapImage(new Uri(s1, UriKind.Absolute));

                };
                Image_b.ImageFailed += (s, args) =>
                {
                    var s1 = MainPage.current_city.Url + current_line.ImageUrl_B;
                    Image_b.Source = new BitmapImage(new Uri(s1, UriKind.Absolute));

                };
                Header_a.ImageFailed += (s, args) =>
                {
                    var s1 = MainPage.current_city.Url + current_line.Header_A;
                    Header_a.Source = new BitmapImage(new Uri(s1, UriKind.Absolute));

                };
                Header_b.ImageFailed += (s, args) =>
                {
                    var s1 = MainPage.current_city.Url + current_line.Header_B;
                    Header_b.Source = new BitmapImage(new Uri(s1, UriKind.Absolute));

                };
            }
        }

        private void Stops_List_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Stops_List.SelectedIndex = -1;
        }

    }
}